﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TierApplication;
using System.Text;
using System.Data;
using System.Net;
using System.IO;

public partial class ForeProgram_GetPassword : System.Web.UI.Page
{
    private string Mail = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            GenerateRandomCode();
    }

    private void GenerateRandomCode()
    {
        Response.Cookies["AreYouHuman"].Value = CaptchaImage.GenerateRandomCode();
        Img1.Src = "~/ForeProgram/Modules/CaptchaImage/JpegImage.aspx";
    }
    protected void btnValidator_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["AreYouHuman"] != null)
        {
            if (validator.Text == Request.Cookies["AreYouHuman"].Value)
            {
                T_ACCOUNT account = new T_ACCOUNT();
                if (account.Exists(name.Text))
                {
                    T_MAILSENDREC ent = new T_MAILSENDREC();
                    ent.ACCOUNTID = name.Text;
                    ent.MAILCONTENT = GetHtml();
                    ent.MAILCREATEDATE = DateTime.Now;
                    ent.MAILSUB = "乐随行邮件通知中心";
                    ent.RECEIVER = Mail;
                    string result=string.Empty;
                    if (ent.SendEMail(ent, ref result))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "aaa", "<script>$('#main').hide();$('#sendEmailSucceed').show(); </script>");
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "aaa", "<script>alert('邮件发送失败，原因：" + result + "');</script>");
                    }
                    GenerateRandomCode();
                    return;
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "aaa", "<script>Biz.Common.ValidateHelper.warningMessage($('#name'),'GetPassword_Alert_CustomerNotExists'); </script>");
                }
            }
        }
        Page.ClientScript.RegisterStartupScript(this.GetType(), "aaa", "<script>Biz.Common.ValidateHelper.warningMessage($('#validator'),'LoginArea_LoginValiatorError'); </script>");
        GenerateRandomCode();
    }

    private string GetHtml()
    {
        T_ACCOUNT account = new T_ACCOUNT();
        DataSet ds = account.GetAccount(name.Text);
        StringBuilder sb =new StringBuilder();
        string pwd = string.Empty;
        string UserName = string.Empty;
        if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
        {
            pwd = ds.Tables[0].Rows[0]["pwd"].ToString();
            Mail = ds.Tables[0].Rows[0]["EMAIL"].ToString();
            UserName = ds.Tables[0].Rows[0]["ACCOUNTNAME"].ToString();
        }
        sb.Append(@"<div id=':a3'>");
        sb.Append(@"<div>");
        sb.Append(@"<h3>" + account.ACCOUNTNAME + "，您好</h3>");
        sb.Append(@"<div>");
        sb.Append(@"系统已自动为您重置了新密码。");
        sb.Append(@"<br>");
        sb.Append(@"您的用户名为：" + UserName);
        sb.Append(@"<br>");
        sb.Append(@"您的密码为：" + pwd);
        sb.Append(@"<br>");
        sb.Append(@"<br>");
        sb.Append(@"<br>");
        sb.Append(@"此邮件为系统自动发送，请勿直接回复！");
        sb.Append(@"<br>");
        sb.Append(@"如有任何问题，请与我们联系！");
        sb.Append(@"<br>");
        sb.Append(@"<br>");
        sb.Append(@"<br>");
        sb.Append(@"<a target='_blank' href='" + WebPath.GetRootURI() + "/ForeProgram/Login.aspx" + "'>乐随行</a>");
        sb.Append(@"客户部");
        sb.Append(@"<br>");
        sb.Append(@"咨询电话：" + account.GET_400HOTLINE());
        sb.Append(@"</div>");
        sb.Append(@"</div>");
        sb.Append(@"</div>");
        return sb.ToString();
    }
    protected void lbChange_Click(object sender, EventArgs e)
    {
        GenerateRandomCode();
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder Html = new StringBuilder();
        try
        {
            HttpWebRequest myRq = (HttpWebRequest)HttpWebRequest.Create(WebPath.GetRootURI() + "/ForeProgram/SendMail.aspx?OrderID=0000000040&AccountID=Derry" );
            HttpWebResponse myRp = null;
            StreamReader sr = null;
            Stream mystr = null;
            myRp = (HttpWebResponse)myRq.GetResponse();
            mystr = myRp.GetResponseStream();
            sr = new StreamReader(mystr, Encoding.GetEncoding("utf-8"));
            while (sr.Peek() != -1)
            {
                Html.Append(sr.ReadToEnd()).Replace("\r\n","");
            }
        }
        catch (Exception ex)
        { }
    }
}